package com.yuanqi.java.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuanqi.java.dto.PurchaseOrderDetailDTO;
import com.yuanqi.java.dto.PurchaseOrderDetailQueryDTO;
import com.yuanqi.java.dto.PurchaseOrderDetailVO;
import com.yuanqi.java.entity.PurchaseOrderDetail;
import com.yuanqi.java.result.Result;
import com.yuanqi.java.service.IPurchaseOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 采购订单明细表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-01-27
 */
@RestController
@RequestMapping("/purchase-order-detail")
public class PurchaseOrderDetailController {

    @Autowired
    private IPurchaseOrderDetailService purchaseOrderDetailService;

    /**
     * 分页查询采购明细
     */
    @GetMapping("/page")
    public Result<Map<String, Object>> page(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size,
            @RequestParam(required = false) String supplierInfo,
            @RequestParam(required = false) String orderNo,
            @RequestParam(required = false) String productName,
            @RequestParam(required = false) String purchaseDateStart,
            @RequestParam(required = false) String purchaseDateEnd,
            @RequestParam(required = false) String specification,
            @RequestParam(required = false) String unit
    ) {
        Page<com.yuanqi.java.entity.PurchaseOrderDetail> page = new Page<>(current, size);
        
        PurchaseOrderDetailQueryDTO queryDTO = new PurchaseOrderDetailQueryDTO()
                .setSupplierInfo(supplierInfo)
                .setOrderNo(orderNo)
                .setProductName(productName)
                .setPurchaseDateStart(purchaseDateStart != null ? java.time.LocalDate.parse(purchaseDateStart) : null)
                .setPurchaseDateEnd(purchaseDateEnd != null ? java.time.LocalDate.parse(purchaseDateEnd) : null)
                .setSpecification(specification)
                .setUnit(unit);
        
        Page<PurchaseOrderDetailVO> result = purchaseOrderDetailService.pageQuery(page, queryDTO);
        
        Map<String, Object> data = new HashMap<>();
        data.put("records", result.getRecords());
        data.put("total", result.getTotal());
        data.put("current", result.getCurrent());
        data.put("size", result.getSize());
        
        return Result.success(data);
    }

    /**
     * 查询功能 - 根据条件查询采购明细
     */
    @PostMapping("/query")
    public Result<Map<String, Object>> query(@RequestBody PurchaseOrderDetailQueryDTO queryDTO) {
        Page<com.yuanqi.java.entity.PurchaseOrderDetail> page = new Page<>(1, 1000); // 查询所有匹配结果
        Page<PurchaseOrderDetailVO> result = purchaseOrderDetailService.pageQuery(page, queryDTO);
        
        Map<String, Object> data = new HashMap<>();
        data.put("records", result.getRecords());
        data.put("total", result.getTotal());
        
        return Result.success(data);
    }

    /**
     * 重置查询信息
     */
    @PostMapping("/reset")
    public Result<String> reset() {
        return Result.success("查询条件已重置");
    }

    /**
     * 获取采购明细列表
     */
    @GetMapping("/list")
    public Result<List<PurchaseOrderDetailVO>> getPurchaseDetailList() {
        Page<com.yuanqi.java.entity.PurchaseOrderDetail> page = new Page<>(1, 1000);
        PurchaseOrderDetailQueryDTO queryDTO = new PurchaseOrderDetailQueryDTO();
        Page<PurchaseOrderDetailVO> result = purchaseOrderDetailService.pageQuery(page, queryDTO);
        
        return Result.success(result.getRecords());
    }

    /**
     * 根据采购单ID获取明细
     */
    @GetMapping("/order/{orderId}")
    public Result<List<PurchaseOrderDetail>> getDetailsByOrderId(@PathVariable Long orderId) {
        List<PurchaseOrderDetail> details = purchaseOrderDetailService.getDetailsByOrderId(orderId);
        return Result.success(details);
    }

    /**
     * 新增采购明细
     */
    @PostMapping
    public Result<Boolean> create(@Valid @RequestBody PurchaseOrderDetailDTO detailDTO) {
        boolean result = purchaseOrderDetailService.saveBatchDetails(List.of(detailDTO));
        return Result.success(result);
    }

    /**
     * 批量新增采购明细
     */
    @PostMapping("/batch")
    public Result<Boolean> createBatch(@Valid @RequestBody List<PurchaseOrderDetailDTO> detailDTOs) {
        boolean result = purchaseOrderDetailService.saveBatchDetails(detailDTOs);
        return Result.success(result);
    }

    /**
     * 更新采购明细
     */
    @PutMapping("/{id}")
    public Result<Boolean> update(@PathVariable Long id, @Valid @RequestBody PurchaseOrderDetailDTO detailDTO) {
        PurchaseOrderDetail detail = new PurchaseOrderDetail();
        detail.setId(id);
        detail.setOrderId(detailDTO.getOrderId());
        detail.setProductName(detailDTO.getProductName());
        detail.setSpecification(detailDTO.getSpecification());
        detail.setUnit(detailDTO.getUnit());
        detail.setQuantity(detailDTO.getQuantity());
        detail.setUnitPrice(detailDTO.getUnitPrice());
        detail.setRemark(detailDTO.getRemark());
        
        boolean result = purchaseOrderDetailService.updateById(detail);
        return Result.success(result);
    }

    /**
     * 删除采购明细
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean result = purchaseOrderDetailService.removeById(id);
        return Result.success(result);
    }

    /**
     * 根据采购单ID删除明细
     */
    @DeleteMapping("/order/{orderId}")
    public Result<Boolean> deleteByOrderId(@PathVariable Long orderId) {
        boolean result = purchaseOrderDetailService.deleteByOrderId(orderId);
        return Result.success(result);
    }
} 